
def foobar_main():
    print r"Setting up pygame..."
    pygame.init()
    print "done."
    
    print r"Setting up screen..."
    
    if fullscreen is True:
        screen = pygame.display.set_mode((screenres), pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWPALETTE, 8)
    else:
        screen = pygame.display.set_mode((screenres), pygame.DOUBLEBUF | pygame.HWPALETTE, 8)

    pygame.display.set_caption(gamestring)

    graphics = gfxLayer(screen, textres, charSize)
    interface = gfxLayer(screen, textres, charSize)

    # set up background
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill([0,0,0])

    print "done."

	# we're lazy.. make the mouse visible
    pygame.mouse.set_visible(1)

        # initialize font module
    pygame.font.init()
    pygame.register_quit(pygame.font.quit)


    # initialize the clock
    gametime = pygame.time.Clock()

    G_origin = [0, 0]
    cameraOrigin = [0, 0]

    # set up game objects
    renmin = UnitManager()
    for x in range(0, 4):
        foo = Unit()
        foo.setPos([int(random.random() * 25), int(random.random() * 25)])
        renmin.addUnit(foo)

    terrain = Map([100, 100], graphics)
    
    # run caching functions here

    score_rect = pygame.Rect([0,0,0,0])

    gamequit = False
    frametime = 0
    foo_pos = [0,0]
    footext = ""
    foocolor = [255,255,255]
    selected = None

		pygame.event.pump()

                
        # input ###################################              

        # do mouse input
        mousepos = pygame.mouse.get_pos()
        # calc position of the cursor on the text display
        cursorpos = [int(mousepos[0] / charSize[0]), int(mousepos[1] / charSize[1]) ] 


		for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gamequit = True
            elif event.type == pygame.KEYDOWN:
                pressed = pygame.key.get_pressed()
                if pressed is not None:
                    if pressed[pygame.K_ESCAPE]:	gamequit = True
                    if pressed[pygame.K_F1]:	pygame.display.toggle_fullscreen()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                pressed = pygame.mouse.get_pressed()

                # left button
                if pressed[0]:
                    selected = renmin.selectUnitAt(cursorpos)

                # right button
                if pressed[2]:
                    if selected is not None:
                        # see if the target is a unit or not
                        enemy = renmin.selectUnitAt(cursorpos)
                        if enemy is not None:
                            selected.target = enemy
                            selected.action = "attack"
                        else:
                            selected.target = cursorpos
                            selected.action = "go"
                        
            elif event.type == pygame.MOUSEBUTTONUP:
                pass

		# cycle is : clear, render, flip, physics
		# so that the physics functions get a proper time
        # clear ########################################
        screen.blit(background, (0,0)) #lazy clear
        #ObjMgr.doClear()
        #screen.fill(score_rect)
        
        # render #######################################

        # first draw terrain
        #terrain.drawPortion([0,0], [50,50])
        
        renmin.drawUnits(graphics)
        
        #graphics.blitTo(screen)
        graphics.drawBox("border", [0,textres[1]-6], [20,6], colors.lgray, colors.gray)
        graphics.drawBox("symbol", [1,textres[1]-5], [18,4], colors.gray, colors.black, True)

        # also render the overlay
        if selected is not None:
            graphics.drawString(selected.name, [1,textres[1]-5], colors.white, colors.black)
            graphics.drawString("hp:" + str(selected.hp), [1,textres[1]-4], colors.lgreen, colors.black)
            graphics.drawString("speed:" + str(selected.basespeed), [1,textres[1]-3], colors.lblue, colors.black)
            

        graphics.drawString("frametime:" + str(frametime), [1,textres[1]-2], colors.white, colors.black)

        # if we have a unit selected
        if selected is not None:
            # draw something that makes it look selected
            graphics.drawSurf(selected.wenzi, selected.getPos(), colors.lcyan, colors.cyan)
            
            # if there is a unit under the cursor, we are attacking
            if renmin.selectUnitAt(cursorpos) is not None:
                cursor_wenzi = "attack"     # FIXME: replace with cursor "states" later

            # if there is nothing under the cursor
            else:
                cursor_wenzi = "go"

            # draw a marker for this unit's destination
            if selected.target is not None:
                if isinstance( selected.target, Unit ):
                    graphics.drawSurf("target", selected.target.getPos(), colors.lred, colors.red)
                else:
                    graphics.drawSurf("target", selected.target, colors.lcyan, colors.cyan)

            
        else:
            cursor_wenzi = "pick"

        # draw cursor!
        graphics.drawSurf(cursor_wenzi, cursorpos, colors.lcyan, colors.cyan)


        # DISPLAY FLIP ####################################################
        pygame.display.flip()
        ##if busy_loop is True:
        ##    gametime.tick_busy_loop(desired_fps)
        ##else:
        gametime.tick(desired_fps)

		frametime = gametime.get_time() / 1000.0
		# #################################################################

		# physics, etc. ################################
        for ren in renmin.entities:
                    ren.update(frametime)
                    #ren.goTo(frametime, cursorpos)
    # / while quit is False:
    
    # shut down pygame
    pygame.quit()